<template>
  <div>
    <el-card>
      <el-form :model="searchForm" class="demo-form-inline" label-width="80px" size="mini">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="昵称">
              <el-input v-model="searchForm.nickName" placeholder="昵称" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="用户名">
              <el-input v-model="searchForm.username" placeholder="用户名" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="6" :offset="18">
            <el-button size="mini" @click="reset">重置</el-button>
            <el-button type="primary" size="mini" @click="search">查询</el-button>
          </el-col>
        </el-row>

      </el-form>
    </el-card>

    <!-- 列表 -->
    <el-card>
      <div slot="header" class="clearfix">
        <el-button type="primary" size="mini" @click="add">新增</el-button>
      </div>
      <el-table
        :data="userList"
        border
        style="width: 100%"
      >
        <el-table-column
          fixed
          type="index"
          label="#"
          width="50"
          align="center"
        />
        <el-table-column
          fixed
          prop="username"
          label="用户名"
          width="200"
          align="center"
        />
        <el-table-column
          prop="nick_name"
          label="昵称"
          width="200"
          align="center"
        />
        <el-table-column label="头像" width="200" align="center">
          <template slot-scope="scope">
            <img :src="scope.row.salt" style="width:120px;height:120px">
          </template>
        </el-table-column>
        <el-table-column
          prop="roles"
          label="角色"
          width="80"
          align="center"
        />
        <el-table-column
          prop="create_time"
          label="添加时间"
          width="200"
          align="center"
        />
        <el-table-column
          label="操作"
          width="300"
          align="center"
        >
          <template slot-scope="scope">
            <el-button type="success" size="small" @click="edit(scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="del(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="page.currentPage"
        :page-sizes="page.pageSizes"
        :page-size="page.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />

    </el-card>

    <detail ref="details" />
  </div>
</template>
<script>
import { findUsersByPage, removeUser } from '@/api/auth/user'
import mixin from '@/mixins/index'
import detail from './details/index'
export default {
  components: {
    detail
  },
  mixins: [mixin],
  data() {
    return {
      userList: [],
      searchForm: {
        nickName: '',
        username: ''
      }
    }
  },
  computed: {
  },
  created() {
    this.getUserList()
  },
  methods: {
    reset() {
      this.searchForm = this.$options.data().searchForm
    },
    search() {
      this.getUserList()
    },
    getUserList() {
      findUsersByPage(this.page.currentPage, this.page.size).then(res => {
        if (res.success) {
          this.userList = res.data.rows
          this.page.total = res.data.total
        } else {
          this.$message.error('获取信息失败')
        }
      })
    },
    handleSizeChange(val) {
      this.page.size = val
    },
    handleCurrentChange(val) {
      this.page.currentPage = val
    },
    add() {
      this.$refs.details.open()
    },
    edit(val) {
      this.$refs.details.open(val.id)
    },
    del(val) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        removeUser(val.id).then(res => {
          if (res.success) {
            this.$message.success('删除成功')
            this.getUserList()
          } else {
            this.$message.error('删除失败')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
<style lang="scss" scoped>
  .el-card{
    margin: 30px;
  }
</style>
